package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IDoResumeService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IResumeService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.BeanTools;
import com.jsmtr.rms.utils.ResultBean;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2019/4/18 9:20
 * @modified by:
 */
@Controller
public class DUserResumeSyncResumeController {

    @Autowired
    private IDoResumeService doResumeService;

    @Autowired
    private IResumeService resumeService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IUtilService utilService;

    public IDoResumeService getDoResumeService() {
        return this.doResumeService;
    }

    public void setDoResumeService(IDoResumeService doResumeService) {
        this.doResumeService = doResumeService;
    }

    public IResumeService getResumeService() {
        return this.resumeService;
    }

    public void setResumeService(IResumeService resumeService) {
        this.resumeService = resumeService;
    }

    public IProjectService getProjectService() {
        return this.projectService;
    }

    public void setProjectService(IProjectService projectService) {
        this.projectService = projectService;
    }

    public IUtilService getUtilService() {
        return this.utilService;
    }

    public void setUtilService(IUtilService utilService) {
        this.utilService = utilService;
    }

    //@RequestMapping(value = "/DUserResume/syncDuserResume2")
    public ResultBean syncUserResume(HttpServletRequest request, HttpServletResponse response){
        ResultBean resultBean =new ResultBean();
        String fid=request.getParameter("fid");
        String proid=request.getParameter("proid");
        String userid=request.getParameter("userid");
        String cardno=request.getParameter("cardno");
        ProjectInfo projectInfo;
        ProjectInfoFlow projectInfoFlow;
        try {
            if (StringTools.trimNotEmpty(proid)) {
                projectInfo = projectService.getpProjectById(proid);
            }
            if (StringTools.trimNotEmpty(fid)) {
                projectInfoFlow = projectService.getFolwById(fid);
                if (projectInfoFlow!=null){
                    String flowname=projectInfoFlow.getFname()==null?projectInfoFlow.getNickname():projectInfoFlow.getFname();
                    //仅简历筛选和面试环节可以进行简历同步
                    if (flowname.equalsIgnoreCase("简历筛选")|flowname.equalsIgnoreCase("面试")){
                        if (StringTools.trimNotEmpty(userid)){
                            DUserResume dUserResume=doResumeService.getDUserResume(userid);

                        }
                    }else{
                        resultBean.setMessage("当前流程不能进行简历同步!");
                    }
                }else{
                       resultBean.setMessage("读取招聘流程失败!");
                }
            }else{
                resultBean.setMessage("应聘者尚未进入招聘流程!");
            }



        } catch (Exception e) {
            e.printStackTrace();
        }

        return  resultBean;
    }

    public StringBuffer CheckResumeInfo(UserResume resume,DUserResume dUserResume) throws Exception {
        StringBuffer reslut=new StringBuffer();
        Class rClzz= resume.getClass();
        Method[] methods= rClzz.getDeclaredMethods();
        for (Method method : methods) {
            if(!method.getName().equalsIgnoreCase("getId")&&!method.getName().equalsIgnoreCase("getUserId")){
                String methodname=method.getName();
               Method dmethod=dUserResume.getClass().getDeclaredMethod(methodname);
               if(dmethod!=null){
                   Object resumeValue=method.invoke(resume,null);
                   Object dresumeValue=method.invoke(dUserResume,null);
                   if(!resumeValue.equals(dresumeValue)){
                       reslut.append("变更:"+methodname+",{resumeValue:"+resumeValue.toString()+",dresumeValue:"+dresumeValue.toString());
                       String setMethodname=dmethod.getName().replaceFirst("get","set");
                       Method setMethod=  dUserResume.getClass().getDeclaredMethod(setMethodname);
                       setMethod.invoke(dUserResume,resumeValue);
                   }
               }
            }
        }
        return  reslut;
    }

    public StringBuffer checkChangeInfo(Object resume,Object dUserResume) throws Exception {
        StringBuffer reslut=new StringBuffer();
        Class rClzz= resume.getClass();
        Method[] methods= rClzz.getDeclaredMethods();
        for (Method method : methods) {
            if(!method.getName().equalsIgnoreCase("getId")&&!method.getName().equalsIgnoreCase("getRid")){
                String methodname=method.getName();
                Method dmethod=dUserResume.getClass().getDeclaredMethod(methodname);
                if(dmethod!=null){
                    Object resumeValue=method.invoke(resume,null);
                    Object dresumeValue=method.invoke(dUserResume,null);
                    if(!resumeValue.equals(dresumeValue)){
                        reslut.append("变更:"+methodname+",{resumeValue:"+resumeValue.toString()+",dresumeValue:"+dresumeValue.toString());
                        String setMethodname=dmethod.getName().replaceFirst("get","set");
                        Method setMethod=  dUserResume.getClass().getDeclaredMethod(setMethodname);
                        setMethod.invoke(dUserResume,resumeValue);
                    }
                }
            }
        }
        return  reslut;
    }

    public StringBuffer CheckEducation(UserResume resume,DUserResume dUserResume) throws Exception {
       StringBuffer reslut=new StringBuffer();
       List<UserEducation> userEducationList= resumeService.getUserEducation(resume.getId());
       List<DUserEducation> dUserEducationList=doResumeService.getUserEducation(dUserResume.getId());
       if (userEducationList.size()>dUserEducationList.size()){
           reslut.append("");
       }else if (userEducationList.size()<dUserEducationList.size()){

       }else{

           for (UserEducation userEducation : userEducationList) {
               //reslut.append(checkChangeInfo(userEducation,));
           }

       }
        return  reslut;
    }

    public StringBuffer CheckWorkExperience(UserResume resume,DUserResume dUserResume){
        StringBuffer result=new StringBuffer();

        return result;
    }

    public StringBuffer CheckTraining(UserResume resume,DUserResume dUserResume){
        StringBuffer result=new StringBuffer();

        return result;
    }

    public StringBuffer CheckFamily(UserResume resume,DUserResume dUserResume){
        StringBuffer result=new StringBuffer();

        return result;
    }

    public StringBuffer CheckRewardPunishment(UserResume resume,DUserResume dUserResume){
        StringBuffer result=new StringBuffer();

        return result;
    }


    @RequestMapping(value = "/DUserResume/syncDuserResume")
    public void syscUserResumeByDel(HttpServletResponse response,HttpServletRequest request){
        try {
            String pid = request.getParameter("proid");//招聘方案ID
            String fid = request.getParameter("fid");//招聘地区编码
            String cardno = request.getParameter("cardno");//招聘地区名称
            String duserid=request.getParameter("userid");
            String vcode=request.getParameter("vcode");

            BaseUser usr = utilService.getSessionUser(request);
            if (usr == null) {
                utilService.printJsonResult(false, "请登录！", "no", response);
                return;
            }

            //黑名单用户不能投简历
            if (!usr.getUdept().equalsIgnoreCase("00006") ) {
                  if(usr.getUcode().equalsIgnoreCase("14638")||usr.getUcode().equalsIgnoreCase("10338")){

                  }else {
                      utilService.printJsonResult(false, "非法访问！", "no", response);
                    return;
                  }
            }
            Object svcode=request.getSession().getAttribute("validcode");
            if (StringTools.trimNotEmpty(vcode)){
                if(!vcode.toLowerCase().equalsIgnoreCase(svcode.toString())){
                    utilService.printJsonResult(false, "验证码错误！", "no", response);
                    return;
                }
            }else{
                utilService.printJsonResult(false, "请输入验证码！", "no", response);
                return;
            }

            ProjectInfoFlow projectInfoFlow=projectService.getFolwById(fid);
            if(projectInfoFlow!=null){
                String flowname=projectInfoFlow.getFname()==null?projectInfoFlow.getNickname():projectInfoFlow.getFname();
                if (!flowname.equalsIgnoreCase("简历筛选")){
                    utilService.printJsonResult(false, "当前招聘流程不能进行同步操作！", "no", response);
                    return;
                }
            }else{
                utilService.printJsonResult(false, "招聘流程读取失败！", "no", response);
                return;
            }
            //20180629 改为通过投递次数判断是否已经投过简历
            /*DUserResume dresume = jobService.getDResume(usr.getId(), pid);
            if (dresume != null) {
                utilService.printJsonResult(false, "不能重复投简历！", "no", response);
                return;
            }*/
            DUserResume oldDuserresume=doResumeService.getDUserResume(duserid);
            UserResume resume;
            if (oldDuserresume!=null){
                resume = resumeService.getUserResume(oldDuserresume.getUserid());
                if (resume == null) {
                    utilService.printJsonResult(false, "请先填写个人简历！", "no", response);
                    return;
                }else{
                    List editList=new ArrayList();
                    List addList=new ArrayList();
                    List delList=new ArrayList();
                    syncUserBaseInfo(editList,oldDuserresume,resume);
                    syncEducationByDel(addList,delList,oldDuserresume,resume);
                    syncWorkexperiencesByDel(addList,delList,oldDuserresume,resume);
                    syncTrainingByDel(addList,delList,oldDuserresume,resume);
                    syncRewardPunishmentByDel(addList,delList,oldDuserresume,resume);
                    syncFamilyByDel(addList,delList,oldDuserresume,resume);
                    syncMajorByDel(addList,delList,oldDuserresume,resume);
                    if (editList.size()>0){
                        utilService.editList(delList,editList,addList);
                        utilService.printJsonResult(true, "简历更新成功！", "no", response);
                    }

                }
            }else {
                utilService.printJsonResult(false, "原投递简历读取失败！", "no", response);
                return;
            }

        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "简历更新失败！", "no", response);
        }
    }

    private void syncUserBaseInfo(List editList,DUserResume oldDuserResume,UserResume resume) throws Exception {
        ProjectInfo project = projectService.getpProjectById(oldDuserResume.getProid());
        DUserResume dUserResume = new DUserResume();
        BeanTools.Copy(resume, dUserResume);
        //String nid = UUID.randomUUID().toString();
        dUserResume.setId(oldDuserResume.getId());
        dUserResume.setAddDate(new Date());
        dUserResume.setState("未处理");
        dUserResume.setSxbz(null);
        //招聘岗位
        dUserResume.setJid(oldDuserResume.getJid());
        dUserResume.setJname(oldDuserResume.getJname());

        //招聘计划
        dUserResume.setPid("-");
        dUserResume.setPname("-");

        //招聘方案
        dUserResume.setProid(oldDuserResume.getProid());
        dUserResume.setProname(oldDuserResume.getProname());

        //招聘流程ID
        ProjectInfoFlow firstFlow=doResumeService.getFirstFlow(oldDuserResume.getProid());
        String fid = firstFlow.getId();
        dUserResume.setFid(fid);
        dUserResume.setPath("/" + fid + "/");
        dUserResume.setFqstate("未放弃");
        dUserResume.setZlmsg("-");
        dUserResume.setZlstate("未提交");
        dUserResume.setZlremark(null);
        dUserResume.setZsstate("未政审");
        dUserResume.setZsremark(null);
        dUserResume.setTjstate("未体检");
        dUserResume.setTjmsg("未发通知");
        dUserResume.setTjremark(null);
        dUserResume.setQystate("未签约");
        dUserResume.setQyremark(null);
        dUserResume.setGwstate("已分配");
        dUserResume.setBdstate("未报到");
        dUserResume.setBdremark(null);
        dUserResume.setBdmsg("未发通知");
        editList.add(dUserResume);



    }
    //教育背景
    private void syncEducationByDel(List addlist,List delList,DUserResume oldDuserResume,UserResume userResume ) throws Exception {

        List<UserEducation> usereducations = resumeService.getUserEducation(userResume.getId());
        List<DUserEducation> oldDUserEducationList=doResumeService.getUserEducation(oldDuserResume.getId());
        if(usereducations!=null){
            for (UserEducation usereducation : usereducations) {
                DUserEducation dUserEducation = new DUserEducation();
                BeanTools.Copy(usereducation, dUserEducation);
                dUserEducation.setId(UUID.randomUUID().toString());
                dUserEducation.setRid(oldDuserResume.getId());
                addlist.add(dUserEducation);
            }
        }
        if (oldDUserEducationList!=null){
            delList.addAll(oldDUserEducationList);
        }

    }

    //工作经验
    private void syncWorkexperiencesByDel(List addlist,List delList,DUserResume oldDuserResume,UserResume userResume ) throws Exception {

        List<DUserWorkExperience> oldDUserWorkExperienceList=doResumeService.getUserWorkExperience(oldDuserResume.getId());
        List<UserWorkExperience> userworkexperiences = resumeService.getUserWorkExperience(userResume.getId());
        if (userworkexperiences!=null){
            for (UserWorkExperience userworkexperience : userworkexperiences) {
                DUserWorkExperience dUserWorkExperience = new DUserWorkExperience();
                BeanTools.Copy(userworkexperience, dUserWorkExperience);
                dUserWorkExperience.setId(UUID.randomUUID().toString());
                dUserWorkExperience.setRid(oldDuserResume.getId());
                addlist.add(dUserWorkExperience);
            }
        }

        if (oldDUserWorkExperienceList!=null){
            delList.addAll(oldDUserWorkExperienceList);
        }

    }


    //培训经历
    private void syncTrainingByDel(List addlist,List delList,DUserResume oldDuserResume,UserResume userResume ) throws Exception {
        List<DUserTraining> oldDUserDUserTrainingList=doResumeService.getUserTraining(oldDuserResume.getId());
        List<UserTraining> usertrainings = resumeService.getUserTraining(userResume.getId());
        if(usertrainings!=null) {
            for (UserTraining usertraining : usertrainings) {
                DUserTraining dUserTraining = new DUserTraining();
                BeanTools.Copy(usertraining, dUserTraining);
                dUserTraining.setId(UUID.randomUUID().toString());
                dUserTraining.setRid(oldDuserResume.getId());
                addlist.add(dUserTraining);
            }
        }
        if (oldDUserDUserTrainingList!=null){
            delList.addAll(oldDUserDUserTrainingList);
        }

    }

    //获奖信息
    private void syncRewardPunishmentByDel(List addlist,List delList,DUserResume oldDuserResume,UserResume userResume ) throws Exception {

        List<DUserRewardPunishment> oldDUserRewardPunishmentList=doResumeService.getUserRewardPunishment(oldDuserResume.getId());
        //获奖信息
        List<UserRewardPunishment> userrewardpunishments = resumeService.getUserRewardPunishment(userResume.getId());
        if(userrewardpunishments!=null) {
            for (UserRewardPunishment userrewardpunishment : userrewardpunishments) {
                DUserRewardPunishment dUserRewardPunishment = new DUserRewardPunishment();
                BeanTools.Copy(userrewardpunishment, dUserRewardPunishment);
                dUserRewardPunishment.setId(UUID.randomUUID().toString());
                dUserRewardPunishment.setRid(oldDuserResume.getId());
                addlist.add(dUserRewardPunishment);
            }
        }
        if (oldDUserRewardPunishmentList!=null){
            delList.addAll(oldDUserRewardPunishmentList);
        }

    }

    private void syncMajorByDel(List addlist,List delList,DUserResume oldDuserResume,UserResume userResume ) throws Exception {

        List<DUserMajor> oldDUserRewardPunishmentList=doResumeService.getUserMajor(oldDuserResume.getId());

        List<UserMajor> userMajorList = doResumeService.getUserMajor1(userResume.getId());
        if (userMajorList != null) {


        for (UserMajor userMajor : userMajorList) {
            DUserMajor dUserMajor = new DUserMajor();
            BeanTools.Copy(userMajor, dUserMajor);
            dUserMajor.setId(UUID.randomUUID().toString());
            dUserMajor.setRid(oldDuserResume.getId());
            addlist.add(dUserMajor);
        }
        }
        if (oldDUserRewardPunishmentList!=null){
            delList.addAll(oldDUserRewardPunishmentList);
        }

    }

    //家庭成员
    private void syncFamilyByDel(List addlist,List delList,DUserResume oldDuserResume,UserResume userResume ) throws Exception {

        List<DUserFamily> oldDUserRewardPunishmentList=doResumeService.getUserFamily(oldDuserResume.getId());

        List<UserFamily> userfamilys = resumeService.getUserFamily(userResume.getId());
        if(userfamilys!=null) {
            for (UserFamily userfamily : userfamilys) {
                DUserFamily dUserFamily = new DUserFamily();
                BeanTools.Copy(userfamily, dUserFamily);
                dUserFamily.setId(UUID.randomUUID().toString());
                dUserFamily.setRid(oldDuserResume.getId());
                addlist.add(dUserFamily);
            }
        }
        if (oldDUserRewardPunishmentList!=null){
            delList.addAll(oldDUserRewardPunishmentList);
        }

    }
}
